package com.liyi.letter.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyi.letter.common.result.Result;
import com.liyi.letter.system.entity.Department;
import com.liyi.letter.system.entity.Employee;
import com.liyi.letter.system.entity.Role;
import com.liyi.letter.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qianxun
 * @version 1.0
 * @date 2023/6/6 16:12
 */
@RestController
@RequestMapping("/role/system")
public class RoleController {
    @Autowired
    private RoleService roleService;
    /**
     *查询所以角色(用于下拉框)
     * @return
     */
    @GetMapping("getAll")
    public Result getAll(){
        List<Role> list=roleService.getAll();
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        return Result.ok(jsonArray);
    }
    /**
     * 根据条件分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page/{page}/{pageSize}")
    public Result page(@PathVariable Integer page, @PathVariable Integer pageSize,String name){
        Page<Role> rolePage = roleService.MyPage(page, pageSize, name);
        return Result.ok(rolePage);
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        boolean b=roleService.deleteById(id);
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("addRole")
    public Result addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if (!save){
            return Result.fail();
        }
        return Result.ok();
    }
    @GetMapping("/getById/{id}")
    public Result<Role> getById(@PathVariable("id") Long id)  {
        Role role = roleService.getById(id);
        if (role != null) {
            return Result.ok(role);
        }
        return Result.fail();
    }
    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("updatedRole")
    public Result updatedRole(@RequestBody Role role){
        boolean b = roleService.updatedRole(role);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
}
